package com.internship.controller;

import java.io.UnsupportedEncodingException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import net.sf.json.JSONObject;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.servlet.ModelAndView;

import com.internship.model.City;
import com.internship.model.DepartmentInfo;
import com.internship.model.EnterpriseInfo;
import com.internship.model.EnterpriseUser;
import com.internship.model.JobInfo;
import com.internship.model.JobStudent;
import com.internship.model.PracticeChange;
import com.internship.model.Province;
import com.internship.model.StudentInfo;
import com.internship.model.TeacherInfo;
import com.internship.page.Page;
import com.internship.service.Const;
import com.internship.service.DepartmentInfoService;
import com.internship.service.EnterpriseInfoService;
import com.internship.service.EnterpriseUserService;
import com.internship.service.JobInfoService;
import com.internship.service.JobStudentService;
import com.internship.service.PracticeChangeService;
import com.internship.service.StudentInfoService;
import com.internship.service.TeacherInfoService;
import com.internship.util.StringUtil;


@Controller
@RequestMapping("practiceChange")
public class PracticeChangeController {
	private final Logger logger = Logger.getLogger(PracticeChangeController.class);
	
	@Autowired
	private PracticeChangeService practiceChangeService;
	@Autowired
	private DepartmentInfoService 	departmentInfoService;
	@Autowired
	private JobStudentService 	jobStudentService;
	@Autowired
	private EnterpriseUserService	enterpriseUserService;
	@Autowired
	private EnterpriseInfoService 	enterpriseinfoService;
	@Autowired
	private TeacherInfoService teacherInfoService;
	@Autowired
	private JobInfoService jobInfoService;
	@Autowired
	private StudentInfoService 	studentInfoService;
	public static HttpSession getSession() {
		HttpSession session = null;
		try {
			session = getRequest().getSession();
		} catch (Exception e) {
		}
		return session;
	}

	public static HttpServletRequest getRequest() {
		ServletRequestAttributes attrs = (ServletRequestAttributes) RequestContextHolder
				.getRequestAttributes();
		return attrs.getRequest();
	}
	private void clearSession() {
		getSession().invalidate();
	}
	/**?
	 * 实习变更列表
	 * @return
	 */
	@RequestMapping("/practiceChange_list.do")
	public ModelAndView getPracticeChangeList() {
		return getPracticeChangeList(null,null,1);
	}
	
	private List<DepartmentInfo>  departmentInfoList = null;
	@RequestMapping("/practiceChange_page_list.do")
	public ModelAndView getPracticeChangeList(@RequestParam String studentName,@RequestParam String deptCode,@RequestParam int pageNumber) {
		ModelAndView mv = new ModelAndView("practice_change");
		HttpSession session = getSession();
		try {
			if (String.valueOf(session.getAttribute(Const.USER_ROLE)).equals("2")||String.valueOf(session.getAttribute(Const.USER_ROLE)).equals("1")) {
				Page<PracticeChange> pageList = practiceChangeService.getPracticeChangeList(studentName, deptCode, null,pageNumber);
				this.departmentInfoList = departmentInfoService.getDepartmentInfoList();
				mv.addObject("currentPage", pageNumber);
				mv.addObject("deptCode", deptCode);
				mv.addObject("totalPage", pageList.getTotalPage());
				mv.addObject("departmentInfoList",departmentInfoList);
				mv.addObject("totalCount", pageList.getTotalCount());
				mv.addObject("practiceChangeList", pageList.getResultList());
			}else if (String.valueOf(session.getAttribute(Const.USER_ROLE)).equals("3") ){
				Page<PracticeChange> pageList = practiceChangeService.getPracticeChangeList(studentName,(String.valueOf(session.getAttribute(Const.USER_DEPARTMENT_CODE))), null, pageNumber);
				mv.addObject("currentPage", pageNumber);
				mv.addObject("totalPage", pageList.getTotalPage());
				mv.addObject("totalCount", pageList.getTotalCount());
				mv.addObject("practiceChangeList", pageList.getResultList());
			}else {
				Page<PracticeChange> pageList = practiceChangeService.getPracticeChangeList(studentName,(String.valueOf(session.getAttribute(Const.USER_DEPARTMENT_CODE))), (String.valueOf(session.getAttribute(Const.SESSION_ACCOUNT))), pageNumber);
				mv.addObject("currentPage", pageNumber);
				mv.addObject("totalPage", pageList.getTotalPage());
				mv.addObject("totalCount", pageList.getTotalCount());
				mv.addObject("practiceChangeList", pageList.getResultList());
			}
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		mv.addObject("studentName", studentName);
		return mv;
	}
	
	/**?
	 * 我的实习变更
	 * @return
	 */
	@RequestMapping("/myPracticeChange_list.do")
	public ModelAndView myPracticeChangeList() {
		return myPracticeChangeList(1);
	}
	
	@RequestMapping("/myPracticeChange_page_list.do")
	public ModelAndView myPracticeChangeList(@RequestParam int pageNumber) {
		ModelAndView mv = new ModelAndView("my_practice_change");
		HttpSession session = getSession();
		try {
			JobStudent jobStudent =jobStudentService.getJobStudent(String.valueOf(session.getAttribute(Const.SESSION_ACCOUNT)));
			if (jobStudent!=null) {
				Page<PracticeChange> pageList = practiceChangeService.getMyPracticeChangeList((String.valueOf(session.getAttribute(Const.SESSION_ACCOUNT))), pageNumber);
				mv.addObject("currentPage", pageNumber);
				mv.addObject("totalPage", pageList.getTotalPage());
				mv.addObject("totalCount", pageList.getTotalCount());
				mv.addObject("practiceChangeList", pageList.getResultList());
			}else {
				 mv.addObject("msg", " 您可能还未参加实习,无法申请实习变更!");
				 mv.setViewName("error");
			}
				
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return mv;
	}
	/**?
	 * 查看实习变更
	 * @return
	 */
	@RequestMapping("/practice_change_check.do")
	public ModelAndView practiceChangeCheck(@RequestParam int id) {
		ModelAndView mv = new ModelAndView("practice_change_check");
		PracticeChange practiceChange = practiceChangeService.getPracticeChangeById(id);
		 mv.addObject("practiceChange", practiceChange);
		return mv;
	}
	/**?
	 *审核实习变更
	 * @return
	 */
	@RequestMapping("/practice_change_review.do")
	public ModelAndView practiceChangeReview(@RequestParam int id) {
		ModelAndView mv = new ModelAndView("practice_change_review");
		PracticeChange practiceChange = practiceChangeService.getPracticeChangeById(id);
		 mv.addObject("practiceChange", practiceChange);
		return mv;
	}
	@RequestMapping("/review.do")
	@ResponseBody
	public JSONObject review(@RequestParam("id")int id ,@RequestParam("checkApprove")int checkApprove ,@RequestParam("declineReason")String declineReason  ) {
		JSONObject json = new JSONObject();
		HttpSession session = getSession();
		PracticeChange practiceChange = practiceChangeService.getPracticeChangeById(id);
		try {
		if(Integer.parseInt(String.valueOf(session.getAttribute(Const.USER_ROLE)))<4){
			if(checkApprove==0){
				practiceChange.setCheckApprove(4);
				int rel =practiceChangeService.reviewPracticeChange(practiceChange);
				if (rel>0) {
				JobStudent jobStudent = jobStudentService.getJobStudent(practiceChange.getStudentId());
				if (practiceChange.getToStatus()==2||practiceChange.getToStatus()==3) {
					jobStudent.setJobId(0);
					jobStudent.setJobName("");
					jobStudent.setPracticeEnterprise(practiceChange.getEnterpriseId());
					jobStudent.setEnterpriseName(practiceChange.getEnterpriseName());
					jobStudent.setEnterTeacherId("");
					jobStudent.setEnterTeacherName("");
					jobStudent.setEnterTeacherPhone("");
					jobStudent.setStartTime(practiceChange.getStartTime());
					jobStudent.setEndTime(practiceChange.getEndTime());
					jobStudent.setStatus(practiceChange.getToStatus());
				}else if (practiceChange.getToStatus()==1) {
					jobStudent.setJobId(0);
					jobStudent.setJobName("");
					jobStudent.setPracticeEnterprise(0);
					jobStudent.setEnterpriseName(practiceChange.getEnterpriseName());
					jobStudent.setEnterTeacherId("");
					jobStudent.setEnterTeacherName("");
					jobStudent.setEnterTeacherPhone("");
					jobStudent.setStartTime(practiceChange.getStartTime());
					jobStudent.setEndTime(practiceChange.getEndTime());
					jobStudent.setStatus(practiceChange.getToStatus());
				}else {
					jobStudent.setJobId(practiceChange.getJobId());
					jobStudent.setJobName(practiceChange.getJobName());
					jobStudent.setPracticeEnterprise(practiceChange.getEnterpriseId());
					jobStudent.setEnterpriseName(practiceChange.getEnterpriseName());
					jobStudent.setEnterTeacherId(practiceChange.getEnterTeacherId());
					jobStudent.setEnterTeacherName(practiceChange.getEnterTeacherName());
					jobStudent.setEnterTeacherPhone(practiceChange.getEnterTeacherPhone());
					jobStudent.setStartTime(practiceChange.getStartTime());
					jobStudent.setEndTime(practiceChange.getEndTime());
					jobStudent.setStatus(practiceChange.getToStatus());
				}
				jobStudentService.updateJobStudent(jobStudent);
				}
				json.put("result", rel>0?"success":"error");
			}else {
				practiceChange.setCheckApprove(3);
				practiceChange.setDeclineReason(declineReason);
				int rel =practiceChangeService.reviewPracticeChange(practiceChange);
				json.put("result", rel>0?"success":"error");
			}
		}else {
			if(checkApprove==0){
				practiceChange.setCheckApprove(2);
				int rel =practiceChangeService.reviewPracticeChange(practiceChange);
				if (rel>0) {
					JobStudent jobStudent = jobStudentService.getJobStudent(practiceChange.getStudentId());
					if (practiceChange.getToStatus()==2||practiceChange.getToStatus()==3) {
						jobStudent.setJobId(0);
						jobStudent.setJobName("");
						jobStudent.setPracticeEnterprise(practiceChange.getEnterpriseId());
						jobStudent.setEnterpriseName(practiceChange.getEnterpriseName());
						jobStudent.setEnterTeacherId("");
						jobStudent.setEnterTeacherName("");
						jobStudent.setEnterTeacherPhone("");
						jobStudent.setStartTime(practiceChange.getStartTime());
						jobStudent.setEndTime(practiceChange.getEndTime());
						jobStudent.setStatus(practiceChange.getToStatus());
					}else if (practiceChange.getToStatus()==1) {
						jobStudent.setJobId(0);
						jobStudent.setJobName("");
						jobStudent.setPracticeEnterprise(0);
						jobStudent.setEnterpriseName(practiceChange.getEnterpriseName());
						jobStudent.setEnterTeacherId("");
						jobStudent.setEnterTeacherName("");
						jobStudent.setEnterTeacherPhone("");
						jobStudent.setStartTime(practiceChange.getStartTime());
						jobStudent.setEndTime(practiceChange.getEndTime());
						jobStudent.setStatus(practiceChange.getToStatus());
					}else {
						jobStudent.setJobId(practiceChange.getJobId());
						jobStudent.setJobName(practiceChange.getJobName());
						jobStudent.setPracticeEnterprise(practiceChange.getEnterpriseId());
						jobStudent.setEnterpriseName(practiceChange.getEnterpriseName());
						jobStudent.setEnterTeacherId(practiceChange.getEnterTeacherId());
						jobStudent.setEnterTeacherName(practiceChange.getEnterTeacherName());
						jobStudent.setEnterTeacherPhone(practiceChange.getEnterTeacherPhone());
						jobStudent.setStartTime(practiceChange.getStartTime());
						jobStudent.setEndTime(practiceChange.getEndTime());
						jobStudent.setStatus(practiceChange.getToStatus());
					}
					jobStudentService.updateJobStudent(jobStudent);
					}
				json.put("result", rel>0?"success":"error");
			}else {
				practiceChange.setCheckApprove(1);
				practiceChange.setDeclineReason(declineReason);
				int rel =practiceChangeService.reviewPracticeChange(practiceChange);
				json.put("result", rel>0?"success":"error");
			}
		}
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return json;
	}
	 private List<Province>  provinceList = null;
		private List<City>  cityList = null;
		private List<EnterpriseUser>  enterpriseUserList = null;
		private List<TeacherInfo>  teacherInfoList = null;
		private List<JobInfo>  jobInfoList = null;
	/**?
	 * 实习变更添加	
	 * @return
	 */
		@RequestMapping("/my_practice_change_add.do")
		public ModelAndView practiceChangeAdd() {
		ModelAndView mv = new ModelAndView("my_practice_change_add");
		HttpSession session = getSession();
		try{
		JobStudent jobStudent = jobStudentService.getJobStudent(String.valueOf(session.getAttribute(Const.SESSION_ACCOUNT)));
		 mv.addObject("jobStudent", jobStudent);
		 	this.provinceList = enterpriseinfoService.getProvince();
			this.cityList = enterpriseinfoService.getCity(provinceList.get(0).getProvinceId());
			List<EnterpriseInfo> enterpriseinfoList = enterpriseinfoService.getEnterpriseinfoEnterList(provinceList.get(0).getProvinceId(), cityList.get(0).getCityId());
			this.enterpriseUserList = enterpriseUserService.getDistributionEnterpriseuserList(enterpriseinfoList.get(0).getId());
			this.teacherInfoList = teacherInfoService.getDistributionTeacherInfoList(String.valueOf(session.getAttribute(Const.USER_DEPARTMENT_NAME)));
			this.jobInfoList = jobInfoService.getDistributionJobinfoList(String.valueOf(session.getAttribute(Const.USER_DEPARTMENT_CODE)));
			mv.addObject("provinceList",provinceList);
			mv.addObject("cityList",cityList);
			mv.addObject("enterpriseinfoList",enterpriseinfoList);
			mv.addObject("enterpriseUserList",enterpriseUserList);
			mv.addObject("teacherInfoList",teacherInfoList);
			mv.addObject("jobInfoList",jobInfoList);
		} catch (Exception ex) {
		ex.printStackTrace();
	}
		return mv;
	}
	 /**?
	  * 自训企业和专升本  申请
	  * @param EnterpriseName
	  * @param TheAddress
	  * @param TheContact
	  * @param TheContactPhone
	  * @param SchoolTeacher
	  * @param startTime
	  * @param endTime
	  * @return
	  */
	 @RequestMapping("/selfEnterprise_save.do")
		@ResponseBody
		public JSONObject selfEnterprise(@RequestParam("Status") int Status,@RequestParam("EnterpriseName") String EnterpriseName,@RequestParam("TheAddress") String TheAddress,@RequestParam("TheContact") String TheContact,@RequestParam("TheContactPhone") String TheContactPhone,@RequestParam("SchoolTeacher") String SchoolTeacher ,@RequestParam("applyCause") String applyCause,@RequestParam("startTime") String startTime ,@RequestParam("endTime") String endTime ) {
			JSONObject json = new JSONObject();
			HttpSession session = getSession();
			try {
				EnterpriseInfo enterpriseInfo = new EnterpriseInfo();
				enterpriseInfo.setName(EnterpriseName);
				enterpriseInfo.setTheCity(0);
				enterpriseInfo.setTheProvince(0);
				enterpriseInfo.setTheAddress(TheAddress);
				enterpriseInfo.setTheContact(TheContact);
				enterpriseInfo.setTheContactPhone(TheContactPhone);
				enterpriseInfo.setCreateType(1);
				int rel = enterpriseinfoService.addEnterpriseinfo(enterpriseInfo);
				if (rel>0) {
					TeacherInfo teacherInfo = teacherInfoService.geTeacherInfoByZGH(SchoolTeacher);
					StudentInfo studentInfo = studentInfoService.getStudentInfoByXH(String.valueOf(session.getAttribute(Const.SESSION_ACCOUNT)));
					PracticeChange practiceChange = new PracticeChange();
					practiceChange.setStudentId(studentInfo.getXH());
					practiceChange.setStudentName(studentInfo.getXM());
					practiceChange.setDeptCode(String.valueOf(session.getAttribute(Const.USER_DEPARTMENT_CODE)));
					practiceChange.setDeptName(String.valueOf(session.getAttribute(Const.USER_DEPARTMENT_NAME)));
					practiceChange.setEnterpriseId(enterpriseInfo.getId());
					practiceChange.setEnterpriseName(EnterpriseName);
					practiceChange.setTeacherId(SchoolTeacher);
					practiceChange.setTeacherName(teacherInfo.getXM());
					practiceChange.setStartTime(startTime);
					practiceChange.setEndTime(endTime);
					practiceChange.setApplyCause(applyCause);
					practiceChange.setApplyTime(StringUtil.getStrDateTime());
					practiceChange.setTheAddress(TheAddress);
					practiceChange.setTheContact(TheContact);
					practiceChange.setThePhone(TheContactPhone);
					practiceChange.setToStatus(Status);
					practiceChangeService.addPracticeChange(practiceChange);
				}
				json.put("result", rel>0?"success":"error");
			} catch (Exception ex) {
				ex.printStackTrace();
			}
			return json;
		}
	 /**?
	  * 学校分配  申请
	  * @param XH
	  * @param PracticeEnterprise
	  * @param EnterTeacherId
	  * @param JobId
	  * @param SchoolTeacher
	  * @param startTime
	  * @param endTime
	  * @return
	  */
	 @RequestMapping("/school_assignment.do")
		@ResponseBody
		public JSONObject schoolAssignment(@RequestParam("PracticeEnterprise") int PracticeEnterprise,@RequestParam("EnterTeacherId") String EnterTeacherId,@RequestParam("JobId") int JobId,@RequestParam("SchoolTeacher") String SchoolTeacher,@RequestParam("applyCause") String applyCause,@RequestParam("startTime") String startTime ,@RequestParam("endTime") String endTime ) {
			JSONObject json = new JSONObject();
			HttpSession session = getSession();
			try {
					EnterpriseInfo enterpriseInfo= 	 enterpriseinfoService.getEnterpriseinfo(PracticeEnterprise);
					try {
						EnterTeacherId = new String(EnterTeacherId.getBytes("ISO-8859-1"),"UTF-8");
					} catch (UnsupportedEncodingException e) {
						e.printStackTrace();
					}
					EnterpriseUser enterpriseUser = enterpriseUserService.getEnterpriseuser(EnterTeacherId);
					JobInfo jobInfo = jobInfoService.getJobinfo(JobId);
					TeacherInfo teacherInfo = teacherInfoService.geTeacherInfoByZGH(SchoolTeacher);
					StudentInfo studentInfo = studentInfoService.getStudentInfoByXH(String.valueOf(session.getAttribute(Const.SESSION_ACCOUNT)));
					PracticeChange practiceChange = new PracticeChange();
					practiceChange.setStudentId(studentInfo.getXH());
					practiceChange.setStudentName(studentInfo.getXM());
					practiceChange.setDeptCode(String.valueOf(session.getAttribute(Const.USER_DEPARTMENT_CODE)));
					practiceChange.setDeptName(String.valueOf(session.getAttribute(Const.USER_DEPARTMENT_NAME)));
					practiceChange.setEnterpriseId(enterpriseInfo.getId());
					practiceChange.setEnterpriseName(enterpriseInfo.getName());
					practiceChange.setTeacherId(SchoolTeacher);
					practiceChange.setTeacherName(teacherInfo.getXM());
					practiceChange.setStartTime(startTime);
					practiceChange.setEndTime(endTime);
					practiceChange.setApplyCause(applyCause);
					practiceChange.setApplyTime(StringUtil.getStrDateTime());
					practiceChange.setTheAddress(enterpriseInfo.getTheAddress());
					practiceChange.setTheContact(enterpriseInfo.getTheContact());
					practiceChange.setThePhone(enterpriseInfo.getTheContactPhone());
					practiceChange.setToStatus(4);
					practiceChange.setJobId(jobInfo.getId());
					practiceChange.setJobName(jobInfo.getJobName());
					practiceChange.setEnterTeacherId(enterpriseUser.getUserAccount());
					practiceChange.setEnterTeacherName(enterpriseUser.getName());
					practiceChange.setEnterTeacherPhone(enterpriseUser.getUserPhone());
				int rel =	practiceChangeService.addPracticeChange(practiceChange);
				json.put("result", rel>0?"success":"error");
			} catch (Exception ex) {
				ex.printStackTrace();
			}
			return json;
}
	 /**?
	  * 创业 申请
	  * @param XH
	  * @param EnterpriseName
	  * @param SchoolTeacher
	  * @param startTime
	  * @param endTime
	  * @return
	  */                                                                   
	 @RequestMapping("/entrepreneurshipEnterprise_save.do")
		@ResponseBody
		public JSONObject entrepreneurshipEnterprise(@RequestParam("XH")String  XH,@RequestParam("EnterpriseName") String EnterpriseName,@RequestParam("SchoolTeacher") String SchoolTeacher,@RequestParam("applyCause") String applyCause,@RequestParam("startTime") String startTime ,@RequestParam("endTime") String endTime ) {
			JSONObject json = new JSONObject();
			try {
					HttpSession session = getSession();
					TeacherInfo teacherInfo = teacherInfoService.geTeacherInfoByZGH(SchoolTeacher);
					StudentInfo studentInfo = studentInfoService.getStudentInfoByXH(String.valueOf(session.getAttribute(Const.SESSION_ACCOUNT)));
					PracticeChange practiceChange = new PracticeChange();
					practiceChange.setStudentId(studentInfo.getXH());
					practiceChange.setStudentName(studentInfo.getXM());
					practiceChange.setDeptCode(String.valueOf(session.getAttribute(Const.USER_DEPARTMENT_CODE)));
					practiceChange.setDeptName(String.valueOf(session.getAttribute(Const.USER_DEPARTMENT_NAME)));
					practiceChange.setEnterpriseName(EnterpriseName);
					practiceChange.setTeacherId(SchoolTeacher);
					practiceChange.setTeacherName(teacherInfo.getXM());
					practiceChange.setStartTime(startTime);
					practiceChange.setEndTime(endTime);
					practiceChange.setApplyCause(applyCause);
					practiceChange.setApplyTime(StringUtil.getStrDateTime());
					practiceChange.setToStatus(4);
				int rel =	practiceChangeService.addPracticeChange(practiceChange);
				json.put("result", rel>0?"success":"error");
			} catch (Exception ex) {
				ex.printStackTrace();
			}
			return json;
}
		/**?
		 * 实习变更修改
		 * @return
		 */
		@RequestMapping("/my_practice_change_edit.do")
		public ModelAndView practiceChangeEdit(@RequestParam int id) {
			ModelAndView mv = new ModelAndView("my_practice_change_edit");
			HttpSession session = getSession();
			try{
				PracticeChange practiceChange = practiceChangeService.getPracticeChangeById(id);
				if(practiceChange.getEnterpriseId()!=0 ){
				EnterpriseInfo enterpriseInfo = enterpriseinfoService.getEnterpriseinfo(practiceChange.getEnterpriseId());
				 mv.addObject("enterpriseInfo", enterpriseInfo);
				}
				 mv.addObject("practicechange", practiceChange);
			JobStudent jobStudent = jobStudentService.getJobStudent(String.valueOf(session.getAttribute(Const.SESSION_ACCOUNT)));
			 mv.addObject("jobStudent", jobStudent);
			 	this.provinceList = enterpriseinfoService.getProvince();
				this.cityList = enterpriseinfoService.getCity(provinceList.get(0).getProvinceId());
				List<EnterpriseInfo> enterpriseinfoList = enterpriseinfoService.getEnterpriseinfoEnterList(provinceList.get(0).getProvinceId(), cityList.get(0).getCityId());
				this.enterpriseUserList = enterpriseUserService.getDistributionEnterpriseuserList(enterpriseinfoList.get(0).getId());
				this.teacherInfoList = teacherInfoService.getDistributionTeacherInfoList(String.valueOf(session.getAttribute(Const.USER_DEPARTMENT_NAME)));
				this.jobInfoList = jobInfoService.getDistributionJobinfoList(String.valueOf(session.getAttribute(Const.USER_DEPARTMENT_CODE)));
				mv.addObject("provinceList",provinceList);
				mv.addObject("cityList",cityList);
				mv.addObject("enterpriseinfoList",enterpriseinfoList);
				mv.addObject("enterpriseUserList",enterpriseUserList);
				mv.addObject("teacherInfoList",teacherInfoList);
				mv.addObject("jobInfoList",jobInfoList);
			} catch (Exception ex) {
			ex.printStackTrace();
		}
			return mv;
		}
		/**?
		 * 修改实习变更申请
		 * @param id
		 * @param PracticeEnterprise
		 * @param EnterTeacherId
		 * @param EnterpriseName
		 * @param entrepreneurshipEnterpriseName
		 * @param TheAddress
		 * @param TheContact
		 * @param TheContactPhone
		 * @param JobId
		 * @param SchoolTeacher
		 * @param applyCause
		 * @param startTime
		 * @param endTime
		 * @return
		 */
		 @RequestMapping("/my_practice_change_update.do")
			@ResponseBody
			public JSONObject myPracticeChangeUpdate(@RequestParam("id") int id,@RequestParam("PracticeEnterprise") int PracticeEnterprise,@RequestParam("EnterTeacherId") String EnterTeacherId,@RequestParam("EnterpriseName") String EnterpriseName,@RequestParam("entrepreneurshipEnterpriseName") String entrepreneurshipEnterpriseName,@RequestParam("TheAddress") String TheAddress,@RequestParam("TheContact") String TheContact,@RequestParam("TheContactPhone") String TheContactPhone,@RequestParam("JobId") int JobId,@RequestParam("SchoolTeacher") String SchoolTeacher,@RequestParam("applyCause") String applyCause,@RequestParam("startTime") String startTime ,@RequestParam("endTime") String endTime ) {
				JSONObject json = new JSONObject();
				PracticeChange  practiceChange = practiceChangeService.getPracticeChangeById(id);
				if (practiceChange.getToStatus()==1) {
					practiceChange.setEnterpriseName(entrepreneurshipEnterpriseName);
					practiceChange.setTeacherId(SchoolTeacher);
					TeacherInfo teacherInfo = teacherInfoService.geTeacherInfoByZGH(SchoolTeacher);
					practiceChange.setTeacherName(teacherInfo.getXM());
					practiceChange.setApplyCause(applyCause);
					practiceChange.setEndTime(endTime);
					practiceChange.setStartTime(startTime);
					practiceChange.setEnterpriseId(0);
					practiceChange.setEnterpriseName(entrepreneurshipEnterpriseName);
					practiceChange.setEnterTeacherId("");
					practiceChange.setEnterTeacherName("");
					practiceChange.setEnterTeacherPhone("");
					practiceChange.setJobId(0);
					practiceChange.setJobName("");
					practiceChange.setTheAddress("");
					practiceChange.setTheContact("");
					practiceChange.setThePhone("");
					int rel =practiceChangeService.editPracticeChange(practiceChange);
					json.put("result", rel>0?"success":"error");
				}else if (practiceChange.getToStatus()==4) {
					EnterpriseUser enterpriseUser = enterpriseUserService.getEnterpriseuser(EnterTeacherId);
					JobInfo jobInfo = jobInfoService.getJobinfo(JobId);
					TeacherInfo teacherInfo = teacherInfoService.geTeacherInfoByZGH(SchoolTeacher);
					EnterpriseInfo enterpriseInfo= 	 enterpriseinfoService.getEnterpriseinfo(PracticeEnterprise);
					practiceChange.setApplyCause(applyCause);
					practiceChange.setEnterpriseId(PracticeEnterprise);
					practiceChange.setEnterpriseName(enterpriseInfo.getName());
					practiceChange.setEnterTeacherId(EnterTeacherId);
					practiceChange.setEnterTeacherName(enterpriseUser.getName());
					practiceChange.setEnterTeacherPhone(enterpriseUser.getUserPhone());
					practiceChange.setJobId(JobId);
					practiceChange.setJobName(jobInfo.getJobName());
					practiceChange.setStartTime(startTime);
					practiceChange.setTeacherId(SchoolTeacher);
					practiceChange.setTeacherName(teacherInfo.getXM());
					practiceChange.setEndTime(endTime);
					practiceChange.setTheAddress(enterpriseInfo.getTheAddress());
					practiceChange.setTheContact(enterpriseInfo.getTheContact());
					practiceChange.setThePhone(enterpriseInfo.getTheContactPhone());
					int rel =practiceChangeService.editPracticeChange(practiceChange);
					json.put("result", rel>0?"success":"error");
				}else {
					TeacherInfo teacherInfo = teacherInfoService.geTeacherInfoByZGH(SchoolTeacher);
					practiceChange.setApplyCause(applyCause);
					practiceChange.setEnterpriseName(EnterpriseName);
					practiceChange.setEnterTeacherId("");
					practiceChange.setEnterTeacherName("");
					practiceChange.setEnterTeacherPhone("");
					practiceChange.setJobId(0);
					practiceChange.setJobName("");
					practiceChange.setStartTime(startTime);
					practiceChange.setTeacherId(SchoolTeacher);
					practiceChange.setTeacherName(teacherInfo.getXM());
					practiceChange.setEndTime(endTime);
					practiceChange.setTheAddress(TheAddress);
					practiceChange.setTheContact(TheContact);
					practiceChange.setThePhone(TheContactPhone);
					int rel =practiceChangeService.editPracticeChange(practiceChange);
					json.put("result", rel>0?"success":"error");
					if(rel>0){
					EnterpriseInfo enterpriseInfo = enterpriseinfoService.getEnterpriseinfo(practiceChange.getEnterpriseId());
					enterpriseInfo.setName(EnterpriseName);
					enterpriseInfo.setTheAddress(TheAddress);
					enterpriseInfo.setTheContact(TheContact);
					enterpriseInfo.setTheContactPhone(TheContactPhone);
					enterpriseinfoService.updateEnterpriseinfo(enterpriseInfo);
					}
				}
					
				return json;
	}
	 /**?
		 * 我的实习变更--手机端
		 * @return
		 */
		@RequestMapping("/moible_myPracticeChange_list.do")
		public ModelAndView moibleMyPracticeChangeList() {
			return moibleMyPracticeChangeList(1);
		}
		
		@RequestMapping("/moible_myPracticeChange_page_list.do")
		public ModelAndView moibleMyPracticeChangeList(@RequestParam int pageNumber) {
			ModelAndView mv = new ModelAndView("moible_my_practice_change");
			HttpSession session = getSession();
			 if (!String.valueOf(session.getAttribute(Const.SESSION_ACCOUNT)).equals("null")) {
			try {
				JobStudent jobStudent =jobStudentService.getJobStudent(String.valueOf(session.getAttribute(Const.SESSION_ACCOUNT)));
				if (jobStudent!=null) {
					Page<PracticeChange> pageList = practiceChangeService.getMyPracticeChangeList((String.valueOf(session.getAttribute(Const.SESSION_ACCOUNT))), pageNumber);
					mv.addObject("currentPage", pageNumber);
					mv.addObject("totalPage", pageList.getTotalPage());
					mv.addObject("totalCount", pageList.getTotalCount());
					mv.addObject("practiceChangeList", pageList.getResultList());
				}else {
					 mv.addObject("msg", " 您可能还未参加实习,无法申请实习变更!");
					 mv.setViewName("error");
				}
			} catch (Exception ex) {
				ex.printStackTrace();
			}
		}else {
			 mv.setViewName("login_moible");
	}
			return mv;
		}
	/**?
	 * 申请变更 -手机端
	 * @return
	 */
		 @RequestMapping("/moible_my_practice_change_add.do")
			public ModelAndView moiblePracticeChangeAdd() {
			ModelAndView mv = new ModelAndView("moible_my_practice_change_add");
			HttpSession session = getSession();
			try{
			JobStudent jobStudent = jobStudentService.getJobStudent(String.valueOf(session.getAttribute(Const.SESSION_ACCOUNT)));
			 mv.addObject("jobStudent", jobStudent);
			 	this.provinceList = enterpriseinfoService.getProvince();
				this.cityList = enterpriseinfoService.getCity(provinceList.get(0).getProvinceId());
				List<EnterpriseInfo> enterpriseinfoList = enterpriseinfoService.getEnterpriseinfoEnterList(provinceList.get(0).getProvinceId(), cityList.get(0).getCityId());
				this.enterpriseUserList = enterpriseUserService.getDistributionEnterpriseuserList(enterpriseinfoList.get(0).getId());
				this.teacherInfoList = teacherInfoService.getDistributionTeacherInfoList(String.valueOf(session.getAttribute(Const.USER_DEPARTMENT_NAME)));
				this.jobInfoList = jobInfoService.getDistributionJobinfoList(String.valueOf(session.getAttribute(Const.USER_DEPARTMENT_CODE)));
				mv.addObject("provinceList",provinceList);
				mv.addObject("cityList",cityList);
				mv.addObject("enterpriseinfoList",enterpriseinfoList);
				mv.addObject("enterpriseUserList",enterpriseUserList);
				mv.addObject("teacherInfoList",teacherInfoList);
				mv.addObject("jobInfoList",jobInfoList);
			} catch (Exception ex) {
			ex.printStackTrace();
		}
			return mv;
		}
			/**?
			 * 实习变更修改 --手机端
			 * @return
			 */
			@RequestMapping("/moible_my_practice_change_edit.do")
			public ModelAndView moiblePracticeChangeEdit(@RequestParam int id) {
				ModelAndView mv = new ModelAndView("moible_my_practice_change_edit");
				HttpSession session = getSession();
				try{
					PracticeChange practiceChange = practiceChangeService.getPracticeChangeById(id);
					if(practiceChange.getEnterpriseId()!=0 ){
					EnterpriseInfo enterpriseInfo = enterpriseinfoService.getEnterpriseinfo(practiceChange.getEnterpriseId());
					 mv.addObject("enterpriseInfo", enterpriseInfo);
					}
					 mv.addObject("practicechange", practiceChange);
				JobStudent jobStudent = jobStudentService.getJobStudent(String.valueOf(session.getAttribute(Const.SESSION_ACCOUNT)));
				 mv.addObject("jobStudent", jobStudent);
				 	this.provinceList = enterpriseinfoService.getProvince();
					this.cityList = enterpriseinfoService.getCity(provinceList.get(0).getProvinceId());
					List<EnterpriseInfo> enterpriseinfoList = enterpriseinfoService.getEnterpriseinfoEnterList(provinceList.get(0).getProvinceId(), cityList.get(0).getCityId());
					this.enterpriseUserList = enterpriseUserService.getDistributionEnterpriseuserList(enterpriseinfoList.get(0).getId());
					this.teacherInfoList = teacherInfoService.getDistributionTeacherInfoList(String.valueOf(session.getAttribute(Const.USER_DEPARTMENT_NAME)));
					this.jobInfoList = jobInfoService.getDistributionJobinfoList(String.valueOf(session.getAttribute(Const.USER_DEPARTMENT_CODE)));
					mv.addObject("provinceList",provinceList);
					mv.addObject("cityList",cityList);
					mv.addObject("enterpriseinfoList",enterpriseinfoList);
					mv.addObject("enterpriseUserList",enterpriseUserList);
					mv.addObject("teacherInfoList",teacherInfoList);
					mv.addObject("jobInfoList",jobInfoList);
				} catch (Exception ex) {
				ex.printStackTrace();
			}
				return mv;
			}
}